/**********************************************************
 * BankAccountAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:45:11 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/
package com.printeverywhere.action;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Preparable;
import com.printeverywhere.delegator.AccountTypeServiceDelegator;
import com.printeverywhere.delegator.BankAccountServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.AccountType;
import com.printeverywhere.dto.Bankaccount;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

/**
 * The Class AccountingManagerAction.
 */
public class BankAccountAction extends FrontBaseSupport implements Preparable,ServletRequestAware{

	public BankAccountAction() {
		super(true, false);
		setMyAccount(true);
	}

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BankAccountAction.class);


	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Bankaccount bankaccount;
	private List<AccountType> accountTypelist = new ArrayList<AccountType>();;
	private Boolean add;
	private List<Bankaccount> useraccounts = new ArrayList<Bankaccount>();
	private User user;
	private int defaultBankId;
	private SearchCriteria searchCriteria = new SearchCriteria();
	/**
	 * @return the defaultBankId
	 */
	public int getDefaultBankId() {
		return defaultBankId;
	}

	/**
	 * @param defaultBankId the defaultBankId to set
	 */
	public void setDefaultBankId(int defaultBankId) {
		this.defaultBankId = defaultBankId;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the useraccounts
	 */
	public List<Bankaccount> getUseraccounts() {
		return useraccounts;
	}

	/**
	 * @param useraccounts the useraccounts to set
	 */
	public void setUseraccounts(List<Bankaccount> useraccounts) {
		this.useraccounts = useraccounts;
	}

	/**
	 * @return the bankaccount
	 */
	public Bankaccount getBankaccount() {
		return bankaccount;
	}

	/**
	 * @param bankaccount the bankaccount to set
	 */
	public void setBankaccount(Bankaccount bankaccount) {
		this.bankaccount = bankaccount;
	}

	/**
	 * @return the accountTypelist
	 */
	public List<AccountType> getAccountTypelist() {
		return accountTypelist;
	}

	/**
	 * @param accountTypelist the accountTypelist to set
	 */
	public void setAccountTypelist(List<AccountType> accountTypelist) {
		this.accountTypelist = accountTypelist;
	}

	/**
	 * @return the add
	 */
	public Boolean getAdd() {
		return add;
	}

	/**
	 * @param add the add to set
	 */
	public void setAdd(Boolean add) {
		this.add = add;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
//	@Override
	public void prepare() throws Exception {
		user=SessionUtil.getUser(request);
		accountTypelist	 = AccountTypeServiceDelegator.getList();
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute(AttributeConstants.USER, SessionUtil.getUser(request));
		useraccounts = BankAccountServiceDelegator.getList(searchCriteria);
	}

	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String addBankAccount() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("addBankAccount() - start"); //$NON-NLS-1$
		}
		if (logger.isDebugEnabled()) {
			logger.debug("addBankAccount() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}


	public String addBankAccountSubmit() throws Exception{
		try{
			bankaccount.setUser(SessionUtil.getUser(request));
			BankAccountServiceDelegator.insertBankaccount(bankaccount);
		}catch (Exception e) {
			addActionError("Error in adding bank account.");
		}
		return SUCCESS;
	}

	public String deleteBankAccount() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("deleteBankAccount() - start"); //$NON-NLS-1$
		}
		try {
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AttributeConstants.ACCOUNTID,
					defaultBankId);
			bankaccount = BankAccountServiceDelegator.getList(searchCriteria)
					.get(AdminConstants.ZERO_NUMBER);
			BankAccountServiceDelegator.deleteBankaccount(bankaccount);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (logger.isDebugEnabled()) {
			logger.debug("deleteBankAccount() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String defaultBankAccount() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("defaultBankAccount() - start"); //$NON-NLS-1$
		}
		user = SessionUtil.getUser(request);
		bankaccount = BankAccountServiceDelegator.getBankaccount(defaultBankId, user);
		request.getSession().setAttribute(AttributeConstants.USER, user);
		if(bankaccount != null) {
			user.setBankaccount(bankaccount);
			UserServiceDelegator.updateUser(user);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("defaultBankAccount() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

//	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;

	}


}